package com.kotlin.android.core

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.kotlin.android.BaseFragment

/**
 *
 * Created on 2020/4/20.
 *
 * @author o.s
 */
abstract class BaseVMFragment<VM : BaseViewModel>(private val useDataBinding: Boolean = true) : BaseFragment() {
    protected var mBinding: ViewDataBinding? = null
    protected var mViewModel: VM? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return if (useDataBinding) {
            mBinding = DataBindingUtil.inflate(inflater, getLayoutResId(), container, false)
            mBinding?.root
        } else {
            inflater.inflate(getLayoutResId(), container, false)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        mViewModel = initVM()
        initView()
        initData()
        startObserve()
        super.onViewCreated(view, savedInstanceState)
    }

    abstract fun getLayoutResId(): Int
    abstract fun initVM(): VM
    abstract fun initView()
    abstract fun initData()
    abstract fun startObserve()

//    /**
//     * 权限申请，依赖的 activity 需继承 [BaseActivity]
//     */
//    fun onRuntimePermissionRequest(
//        permissions: Array<String>, listener: PermissionListener
//    ) = if (activity != null && activity is BaseActivity) {
//        (activity as BaseActivity).onRuntimePermissionsAsk(permissions, listener)
//    } else {
//        throw RuntimeException("容器类不是BaseActivity，检查下")
//    }
}